-- UICommonDesc
-- Create by cheny3 Dec/16/2016
-- 说明界面（蓝标题黑字）

--UICommonDesc继承自Layer
UICommonDesc = class("UICommonDesc", function()
    return cc.Layer:create();
end);

function UICommonDesc.create(title, desc)
    return UICommonDesc.new(title, desc);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 构造函数
function UICommonDesc:ctor(title, desc)
    self:setName("UICommonDesc");

    -- 初始化
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    local node = cc.CSLoader:createNode("layout/workshop/ItemDesc.csb");
    self:addChild(node);
    self.node = node;

    self.title = title;
    self.desc = desc;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UICommonDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UICommonDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 隐藏出售按钮事件
    local btnSell = findChildByName(self.node, "CT/btn_sell");
    btnSell:setVisible(false);

    -- 注册背景点击事件
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UICommonDesc");
        end
    end
    bgImg:addTouchEventListener(onBgClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 重绘
function UICommonDesc:redraw()
    -- 星级
    local starNode = findChildByName(self.node, "CT/star_node");
    starNode:setVisible(false);

    -- 名称
    local nameLabel = findChildByName(self.node, "CT/name");
    TextStyleM.setTextStyle(nameLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BLUE);
    nameLabel:setString(self.title);

    -- 描述
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, self.desc);

    local list =  {nameLabel, descLabel};

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL, list);
end

-- 适配
function UICommonDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end